package com.yxq.mdt.app.modules.bottomsheet;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import com.yxq.mdt.app.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author L
 * @time 2019-10-30
 * @describe 选择性别
 */
public class PersonalSexSheetDialogFragment extends BottomSheetDialogFragment {
    Unbinder unbinder;
    @BindView(R.id.dialog_check_box_sex_nan)
    CheckBox dialogCheckBoxSexNan;
    @BindView(R.id.dialog_check_box_sex_nv)
    CheckBox dialogCheckBoxSexNv;
    @BindView(R.id.dialog_tv_save)
    TextView dialogTvSave;
    int sex = 1;

    public PersonalSexSheetDialogFragment(int sex) {
        this.sex = sex;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置背景透明，才能显示出layout中诸如圆角的布局，否则会有白色底（框）
        setStyle(BottomSheetDialogFragment.STYLE_NORMAL, R.style.CustomBottomSheetDialogTheme);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.personal_sex_bottom_sheet_fragment_dialog, container, false);
        unbinder = ButterKnife.bind(this, view);
        initViews();
        return view;
    }

    private void initViews() {
        dialogCheckBoxSexNan.setChecked(sex == 1);
        dialogCheckBoxSexNv.setChecked(sex == 2);
        dialogCheckBoxSexNan.setOnCheckedChangeListener((compoundButton, b) -> dialogCheckBoxSexNv.setChecked(!b));
        dialogCheckBoxSexNv.setOnCheckedChangeListener((compoundButton, b) -> dialogCheckBoxSexNan.setChecked(!b));
    }


    @OnClick(R.id.dialog_tv_save)
    public void onViewClicked() {
        dismiss();
        if (personalSexSheetDialogFragmentInterface != null) {
            personalSexSheetDialogFragmentInterface.checked(dialogCheckBoxSexNan.isChecked() ? 1 : 2);
        }
    }

    PersonalSexSheetDialogFragmentInterface personalSexSheetDialogFragmentInterface;

    public void setPersonalSexSheetDialogFragmentInterface(PersonalSexSheetDialogFragmentInterface personalSexSheetDialogFragmentInterface) {
        this.personalSexSheetDialogFragmentInterface = personalSexSheetDialogFragmentInterface;
    }

    public interface PersonalSexSheetDialogFragmentInterface {
        void checked(int sex);
    }
}
